﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using BoneSystem.Extensions.Annotations;
using BoneSystem.Extensions.Annotations.InputTypeAnnotations;

namespace BoneSystem.Extensions
{
    public static class ControlHelper 
    {
        public static object GetValuePropertie(object model, string name)
        {
            PropertyInfo prop = null;
            object value = null;
            var names = name.Split('.');

            foreach (var propName in names)
            {
                if (prop == null)
                {
                    prop = model.GetType().GetProperty(propName);
                    value = prop.GetValue(model);
                }
                else
                {
                    prop = prop.PropertyType.GetProperty(propName);
                    value = prop.GetValue(value);
                }

            }
            if (prop == null)
            {
                throw new Exception("Propriedade " + name + " não encontrada.");
            }
            return value;
        }

        public static IEnumerable<CustomAttributeData> GetAttributes(object model, string name)
        {
            PropertyInfo prop = null;
            var names = name.Split('.');
            foreach (var propName in names)
            {
                if (prop == null)
                {
                    prop = model.GetType().GetProperty(propName);
                }
                else
                {
                    prop = prop.PropertyType.GetProperty(propName);
                }

            }
            if (prop == null)
            {
                throw new Exception("Propriedade " + name + " não encontrada.");
            }
            return prop.CustomAttributes;
        }
        public static string GetExpressionText<T, TProperty>(Expression<Func<T, TProperty>> expression)
        {
            return ExpressionHelper.GetExpressionText(expression);
        }
        public enum CustomExtensionsTypeDataEnum
        {
            Text,
            Monetary,
            Date,
            Hour,
            Datetime,
            Integer,
            Float
        }



        public static InputAttribute GetAnnotations<T, TProperty>(HtmlHelper<T> htmlHelper, Expression<Func<T, TProperty>> expression)
        {
            var inputRet = new InputAttribute();
            var name = ExpressionHelper.GetExpressionText(expression);
            var attributes = ControlHelper.GetAttributes(htmlHelper.ViewData.Model, name);
            foreach (var attribute in attributes)
            {

                if (attribute.AttributeType == typeof(CheckBoxAttribute))
                {
                    inputRet = new CheckBoxAttribute();
                }
                if (attribute.AttributeType == typeof(DateTimeAttribute))
                {
                    inputRet = new DateTimeAttribute();
                }
                if (attribute.AttributeType == typeof(EMailAttribute))
                {
                    inputRet = new EMailAttribute();
                }
                if (attribute.AttributeType == typeof(FloatAttribute))
                {
                    inputRet = new FloatAttribute();
                }
                if (attribute.AttributeType == typeof(IntegerAttribute))
                {
                    inputRet = new IntegerAttribute();
                }
                if (attribute.AttributeType == typeof(MonetaryAttribute))
                {
                    inputRet = new MonetaryAttribute();
                }
                if (attribute.AttributeType == typeof(NumberAttribute))
                {
                    inputRet = new NumberAttribute();
                }
                if (attribute.AttributeType == typeof(RadioAttribute))
                {
                    inputRet = new RadioItemAttribute();
                }
                if (attribute.AttributeType == typeof(TextAttribute))
                {
                    inputRet = new TextAttribute();
                }
                if (attribute.AttributeType == typeof(TimeAttribute))
                {
                    inputRet = new TimeAttribute();
                }
                if (attribute.AttributeType == typeof(UrlAttribute))
                {
                    inputRet = new UrlAttribute();
                }
                if (attribute.AttributeType == typeof(AutoCompleteAttribute))
                {
                    inputRet = new AutoCompleteAttribute();
                }
                if (attribute.AttributeType == typeof(FileUploadAttribute))
                {
                    inputRet = new FileUploadAttribute();
                }

                var type = inputRet.GetType();
                if (attribute.NamedArguments != null)
                {
                    foreach (var argument in attribute.NamedArguments)
                    {
                        var prop = type.GetProperty(argument.MemberInfo.Name);
                        // Seta o valor da propriedade via reflection.
                        if (prop != null)
                        {
                            prop.SetValue(inputRet, argument.TypedValue.Value);

                        }

                    }
                }

            }
            return inputRet;
        }
    }
}